package com.zhiyou100.util.message;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 发送短信
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-24 15:40
 */
public class SendMessage {
    private static final String ACCESS_KEY_ID;
    private static final String ACCESS_SECRET;
    private static final String SIGN_NAME;
    private static final String TEMPLATE_CODE;


    static {
        ACCESS_KEY_ID = "LTAI4GJ1tBP3xVtiKMudR6Ps";
        ACCESS_SECRET = "08KpdfnorlKHXyIab4yLS1NPwyqpAo";
        SIGN_NAME = "ssh商城";
        TEMPLATE_CODE = "SMS_203186863";
    }

    /**
     * 给指定的号码发送短息
     *
     * @param phone   号码
     * @param message code
     */
    public static void sendMessage(String phone, String message) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + message + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
